<html>
	<head>
		<meta charset="utf-8">
		<title>Hello MUI</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<!--标准mui.css-->
		<link rel="stylesheet" href="../css/mui.min.css">
		<link rel="stylesheet" href="../css/mui.css" />
		<link rel="stylesheet" href="../css/app.css" />
		<!--App自定义的css-->
		<style type="text/css">
			.top {
				margin-top: 40px;
				text-align: center;
			}
			.top span{
				display: block;
			}
			
			.sinaweibo {
				position: relative;
				width: 60px;
				height: 60px;
				margin-left: calc(50% - 30px);
				background: url(../images/icon-sina.png);  
			}
			.weixin {
				width: 60px;       
				height: 60px; 
				margin-left: calc(50% - 30px);
				background: url(../images/icon-weixin.png);   
			}
			.qq {
				width: 60px;
				height: 60px;
				margin-left: calc(50% - 30px);
				background: url(../images/icon-qq.png);  
			}
		</style>
	</head>
	<body>
		<header class="mui-bar mui-bar-nav white">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">第三方登录</h1>
		</header>
		<div class="mui-content">
			<!--登录区-->
			<div class="mui-input-group" style="margin-top: 10px;">
				<div class="mui-input-row">
					<label>账号</label>
					<input type="text" class="mui-input-clear" placeholder="请输入账号" />
				</div>
				<div class="mui-input-row">
					<label>密码</label>
					<input type="text" class="mui-input-clear" placeholder="请输入密码" />
				</div>
			</div>
			<div class="regiest" style="text-align: center; margin-top: 20px;">
				<button class="mui-btn-blue">注册</button>
				<button class="mui-btn-green">登录</button>
				<button class="mui-btn-red">注销</button>
			</div>
			<div class="top" id="testLogin">
				<span class="mui-icon weixin" id="weixin"></span> 
				<span class="mui-icon qq" id="qq"></span> 
				<span class="mui-icon sinaweibo" id="sinaweibo"></span>
				<button class="mui-btn-purple" style="margin-top: 20px;">绑定第三方账户</button>
			</div>
		</div>
	</body>
	<script src="../js/mui.min.js"></script>
	<script src="../js/mui.zoom.js">	</script>
	<script src="../js/mui.previewimage.js"></script>
	<script src="../js/tools.js" ></script>
	<script>
		// 获取用户头像标签
		var headImage = document.getElementById('headImage');
		
		mui.init({
			swipeBack:true //启用右滑关闭功能
		});
		mui.plusReady(function() {  
			plus.oauth.getServices(function(services) {
				auths = services;
			}, function(e) {
				alert("获取登录服务列表失败：" + e.message + " - " + e.code);
			});
		})
	
		document.getElementById('weixin').addEventListener('tap',function() {
			console.log("微信");
			authLogin('weixin');
		})
		document.getElementById('qq').addEventListener('tap',function() {
			console.log("QQ");
			authLogin('qq');
		})
		document.getElementById('sinaweibo').addEventListener('tap',function() {
			console.log("微博");
			authLogin('sinaweibo');
		})
	
		// 登录操作
		function authLogin(type) {
			var s;
			for (var i = 0; i < auths.length; i++) {
				if (auths[i].id == type) {
					s = auths[i];
					break;
				}
			}
			if (!s.authResult) {
				s.login(function(e) {
					mui.toast("登录认证成功！");
					authUserInfo(type);
				}, function(e) {
					mui.toast("登录认证失败！");
				});
			} else {
				mui.toast("已经登录认证！");
			}
		}
		//注销
		function authLogout() {
			for (var i in auths) {
				var s = auths[i];
				if (s.authResult) {
					s.logout(function(e) {
						console.log("注销登录认证成功！");
					}, function(e) {
						console.log("注销登录认证失败！");
					});
				}
			}
		}
		// 微信登录认证信息
		function authUserInfo(type) {
			var s;
			for (var i = 0; i < auths.length; i++) {
				if (auths[i].id == type) {
					s = auths[i];
					break;
				}
			}
			if (!s.authResult) {
				mui.toast("未授权登录！");
			} else {
				s.getUserInfo(function(e) {
					var josnStr = JSON.stringify(s.userInfo);
					var jsonObj = s.userInfo;
					
					console.log("获取用户信息成功：" + josnStr);
					showData(type,jsonObj);
					authLogout();
				}, function(e) {
					alert("获取用户信息失败：" + e.message + " - " + e.code);
				});
			}
		}
		// 显示用户头像信息
		function showData(type,data) {
			switch (type){
				case 'weixin':
					headImage.src = data.headimgurl;
					break;
				case 'qq':
					headImage.src = data.figureurl_qq_2;
					break;
				case 'sinaweibo':
					headImage.src = data.avatar_large;
					break;
				default:
					break;
			}
		}
	
	
	
	</script>
</html>